package com.example.list;

import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import androidx.appcompat.app.AppCompatActivity;

public class PopwinBasicActivity extends AppCompatActivity {

    private View mPopView;
    private View mPopView2;
    private PopupWindow mPopupWindow;
    private PopupWindow mPopupWindow2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popwin_basic);
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                initPopWindow();
                initPopwin2();
            }
        });
    }

    void initPopwin2(){

        // 将布局文件转换成View对象，popupview 内容视图
        mPopView2 = LayoutInflater.from(this).inflate(R.layout.layout_popup, null);

        BasePopwin basePopwin = new BasePopwin(this, mPopView2);

    }

    private void initPopWindow() {

        // 将布局文件转换成View对象，popupview 内容视图
        mPopView = LayoutInflater.from(this).inflate(R.layout.layout_popup, null);
        ImageView img_close = mPopView.findViewById(R.id.img_close);

        // 将转换的View放置到 新建一个popuwindow对象中
        mPopupWindow = new PopupWindow(mPopView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1);
            }
        });

        setBackgroundAlpha(0.5f);
        // 点击popuwindow外让其消失
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAtLocation(mPopView, Gravity.BOTTOM, 0, 0);
    }
    /**
     * 设置popupwindow外面背景透明度
     *
     * @param bgalpha 透明度  0-1   0-透明   1-不透明
     */
    public void setBackgroundAlpha(float bgalpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgalpha;
        getWindow().setAttributes(lp);
    }

    public void BasicClick(View view) {
        initPopWindow();
    }
}